package com.service.imp;

import com.common.Base;
import com.common.Result;
import com.entity.ArticleData;
import com.repository.ArticleDataRepository;
import com.service.ArticleDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * @Author：Luohuiyong
 * @Description:
 * @Date: Created in 11:06 2019/1/9
 */
@Service
public class ArticleDataServiceImp implements ArticleDataService {

    @Autowired
    private ArticleDataRepository articleDataRepository;

    @Override
    public ArticleData findOne(int id){
        Optional<ArticleData> optionalUser = articleDataRepository.findById(id);
        return optionalUser.get();
    }

    /**
     * 新增数据
     * @param articleData
     * @return
     */
    @Transactional
    @Override
    public Result create(ArticleData articleData){
        articleDataRepository.save(articleData);

        return Base.success();
    }

    /**
     * 喜欢文章
     * @param bean
     * @return
     */
    @Transactional
    @Override
    public Result like(ArticleData bean){
        ArticleData articleData = articleDataRepository.findByArticleId(bean.getArticleId());
        articleData.setLikeNum(articleData.getLikeNum() + 1);
        articleDataRepository.save(articleData);

        return Base.success();
    }

    /**
     * 更新评论数
     * @param bean
     * @return
     */
    @Transactional
    @Override
    public Result updateCommentNum(ArticleData bean){
        ArticleData articleData = articleDataRepository.findByArticleId(bean.getArticleId());
        articleData.setCommentNum(bean.getCommentNum());
        articleDataRepository.save(articleData);

        return Base.success();
    }

    /**
     * 更新阅读数
     * @param bean
     * @return
     */
    @Transactional
    @Override
    public Result updateReadNum(ArticleData bean){
        ArticleData articleData = articleDataRepository.findByArticleId(bean.getArticleId());
        articleData.setReadNum(articleData.getReadNum() + 1);
        articleDataRepository.save(articleData);

        return Base.success();
    }



}
